<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Grid;
use App\Models\GridImg;
use App\Models\ProtocolSet;
use App\Models\BuildForm;
use App\Models\GridArea;
use App\Models\GridHouse;
use App\Models\GridPrice;
use App\Models\GridAssess;
use App\Models\PrintLog;
use App\Models\SignSetType;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Validator;

// 工具
class CommonController extends Controller
{
    // 协议pdf下载
    public function downloadPdf(Request $request)
    {
        $validate = Validator::make($request->all(), [
            'grid_id' => 'required',
            'type' => 'required',
            'sign_set_type' => 'required',
        ], [
            'grid_id.required' => '请输入网格id',
            'type.required' => '请输入类型',
            'sign_set_type.required' => '请输入模板类型',
        ]);

        if ($validate->fails()) {
            return showError($validate->errors()->first());
        }

        $params = $validate->getData();

        if($params['sign_set_type']==1){//签约
            $sign_set_type=SignSetType::query()
            ->where('project_id',auth('api')->user()->project_id ?? 2)
            ->where('type',1)
            ->value('id');
        }elseif($params['sign_set_type']==2){//测绘
            $sign_set_type=SignSetType::query()
            ->where('project_id',auth('api')->user()->project_id ?? 2)
            ->where('template_id',26)
            ->value('id'); 
        }elseif($params['sign_set_type']==3){//评估
            $sign_set_type=SignSetType::query()
            ->where('project_id',auth('api')->user()->project_id ?? 2)
            ->where('template_id',27)
            ->value('id'); 
        }else{
            return showError('模板类型错误');
        }

        if(!$sign_set_type){
            return showError('请联系管理员，配置打印模板');
        }

        $params['sign_set_type']= $sign_set_type;
        $params['print_type']= 9;
        try {
            $data=$this->getProtocolData($params);
            if (!$data) {
                return showError('未录入数据，无法打印');
            }
            if (isset($data['code']) && $data['code'] == 0) {
                return $data;
            }
            //额外打印数据
            $print_data['supply_form'] = $data['supply_form'];
            $print_data['build_form'] = $data['build_form'];
            $print_data['auto_num'] = $data['auto_num'];
            $print_data['number_header'] = $data['number_header'];
            $print_data['sign_set_type'] = $sign_set_type;
           
            // 记录打印数据
            if ($params['type'] == 2) {
                $gridInfo = Grid::query()
                    ->select('id', 'project_id', 'supply_form','sign_time')
                    ->where('id', $params['grid_id'])
                    ->first();
 
                $admin_id = auth('api')->user()->id ?? 0;
                $print_data['grid_id'] = $params['grid_id'];
                $print_data['project_id'] = $gridInfo['project_id'];
                $print_data['print_type'] = 9;
                $print_data['content'] = $data['pdf_url'];
                $print_data['admin_id'] = $admin_id;
                $add = PrintLog::query()->create($print_data);
                // 记录网格是否签约
                if($params['sign_set_type']==1){//签约
                    $update_data['if_sign']=1;
                    $update_data['sign_time']=$gridInfo['sign_time']??date('Y-m-d H:i:s');
                    $update_data['sign_num']=$print_data['number_header'];
                    $update = Grid::query()->where('id', $params['grid_id'])->update($update_data);
                }  
            }

            return showData($data['pdf_url']);
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
       
    }

}

